<template>
    <div class="container">
        <!-- header -->
        <div class="header">
            <div style="display:flex">
                <div
                    class="header-right"
                    style="display:flex">
                    <p class="header-title">选择学校</p>
                    <el-select
                        v-model="school"
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in optionSchool"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-right">
                    <p class="header-title">状态</p>
                    <el-select
                        v-model="status"
                        filterable
                        placeholder="请选择"
                        size="small"
                        clearable
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in optionStatus"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <el-button
                class="feedback-management"
                type="primary"
                size="small"
                @click="resultRouteTofeedbacak()"
            >反馈管理</el-button>

        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
        >
            <el-table-column
                label="ID"
                width="100px"
                type="index"
                align="center"
            />
            <el-table-column
                prop="school_name"
                label="学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="true_name"
                label="用户姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="exam_score"
                label="答题得分"
                show-overflow-tooltip
            />
            <el-table-column
                prop="submit_time"
                label="提交时间"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.submit_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="cost_time"
                label="答题用时"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.cost_time | setTime }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 1">已通过</span>
                    <span v-if="scope.row.status == 0">未通过</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                :width="250"
            >
                <template slot-scope="scope">
                    <span
                        class="curse-add"
                        v-if="scope.row.status == 1"
                    >--</span>
                    <span
                        class="curse-adopt"
                        @click="fnadopt(scope.row)"
                        v-if="scope.row.status == 0"
                    >通过</span>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->
    </div>
</template>

<script>
export default {
    data () {
        return {
            school: '',
            status: '',
            optionSchool: [{
                value: '',
                label: '全部'
            }],
            optionStatus: [{
                value: '',
                label: '全部'
            },{
                value: '1',
                label: '已通过'
            }, {
                value: '0',
                label: '未通过'
            }],
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
        };
    },
    filters: {
        setTime (val) {
            let f = '';
            let s = '';
            let result = '';

            if (val > 60) {
                f = parseInt(val/60);
                s = parseInt(val - f*60);
            } else {
                f = '0';
                s = val;
            }
            result = f + '分' + s + '秒';

            return result;
        }
    },
    created () {
        this.setHeader();
        this.getTableData();
        this.getSchoolList();
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '社交'
            },
            {
                title: "试题结果"
            },
            {
                title: "试题结果"
            }]
            );
        },

        /**
         * 获取已开通学校列表
         */
        getSchoolList () {
            this.$post('/student/forum%5Cget_school_list', {
            }, resp => {
                if (resp.code == 1) {
                    let list = [];

                    resp.data.list.map(item => {
                        list.push({
                            value: item.forum_no,
                            label: item.school_name
                        });
                    });

                    this.optionSchool = this.optionSchool.concat(list);
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/exam_submit%5Cget_list', {
                page_id: page || this.page.now_page,
                status: this.status,
                forum_no: this.school
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 通过
         */
        fnadopt (row) {
            let status = row.status;

            status = status == 1 ? status = 0 : status = 1;

            this.$post('/student/exam_submit%5Cset_status', {
                id: row.id,
            }, resp => {
                if (resp.code == 1) {
                    this.$set(row, 'status', status);
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },
        /**
         * 跳转反馈管理页面
         */
        resultRouteTofeedbacak(id) {
            this.$router.push('feedback-management');
        },


    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        .header {
            margin-bottom: 20px;
            position: relative;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .header-title {
                color: #606266;
                margin-right: 8px;
            }
            .header-right {
                display: flex;
                margin-left: 30px;
                align-items: center;
            }
            .feedback-management{
                float: right;
                margin-right: 50px;
            }
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .curse-adopt {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
    }
</style>
<style lang="less">
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
</style>